package org.ahuanlien.crm.web.controller;

import com.alibaba.fastjson.JSON;
import org.ahuanlien.crm.query.ClaimChartQueryObject;
import org.ahuanlien.crm.query.SalesChartQueryObject;
import org.ahuanlien.crm.service.IChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;


@Controller
@RequestMapping("charts")
public class ChartsController {

    @Autowired
    IChartService chartService;

    @RequestMapping("claimByGroup")
    @ResponseBody
    public List<Map<String,BigDecimal>> claimByGroup(ClaimChartQueryObject qo) {
        List<Map<String,BigDecimal>> maps = new ArrayList<>();
        return chartService.queryByGroupType(qo);
    }


    @RequestMapping("salesByGroup")
    @ResponseBody
    public List<Map<String,BigDecimal>> salesByGroup(SalesChartQueryObject qo) {
        List<Map<String,BigDecimal>> maps = new ArrayList<>();
        return chartService.queryByGroupType(qo);
    }

    @RequestMapping("salesbar")
    public String bar(@ModelAttribute("qo")SalesChartQueryObject qo, Model model){

        /*获取到查询出来的结果集*/
        List<Map<String, BigDecimal>> maps = chartService.queryByGroupType(qo);

        /*获取到qo的 分组字段 ,并且映射到 value ,共享给jsp , 用作副标题*/
        model.addAttribute("groupBy",SalesChartQueryObject.groupBys.get(qo.getGroupType()));

       /* 横坐标:  遍历所有的结果集, 获取到其中的key为 groupBy 对应的value , 封装为list ,转换为json字符串,共享给表单
        纵坐标:  遍历所有的结果集, 获取到其中的key为 totalAmount 对应的value , 封装为list ,转换为json字符串,共享给表单 */
        List groupBys = new LinkedList();
        List totalAmounts = new LinkedList();

        for (Map<String, BigDecimal> map : maps) {
            groupBys.add(map.get("groupType"));
            totalAmounts.add(map.get("totalAmount"));
        }
        model.addAttribute("groupBys", JSON.toJSONString(groupBys));
        model.addAttribute("totalAmounts", JSON.toJSONString(totalAmounts));

        return "reportform/salesbar";
    }

    @RequestMapping("salespie")
    public String pie(@ModelAttribute("qo")SalesChartQueryObject qo, Model model){
        /*获取到查询出来的结果集*/
        List<Map<String, BigDecimal>> maps = chartService.queryByGroupType(qo);

        /*获取到qo的 分组字段 ,并且映射到 value ,共享给jsp , 用作副标题*/
        model.addAttribute("groupBy",SalesChartQueryObject.groupBys.get(qo.getGroupType()));

        /*循环遍历结果集, 查询出分组字段对应的值, 用作name , totalAmount对应的值, 用作value , 存储到新的map之中 ,
         所有的map存储到list之中 , 转换为jsonString ,共享给jsp*/
        List<Map<String,Object>> totalAmounts = new LinkedList();
        BigDecimal max = new BigDecimal(0);
        for (Map<String, BigDecimal> map : maps) {
            /*  {value:335, name:'直接访问'},*/
            if (max.compareTo((BigDecimal)(map.get("totalAmount")))==-1) {
                max = (BigDecimal)(map.get("totalAmount"));
            }
            LinkedHashMap<String, Object> value  = new LinkedHashMap<>();
            value.put("value",map.get("totalAmount"));
            value.put("name",map.get("groupType"));
            totalAmounts.add(value);
        }
        model.addAttribute("max",max);
        model.addAttribute("totalAmounts", JSON.toJSONString(totalAmounts));
        return "reportform/salespie";
    }

    @RequestMapping("claimsbar")
    public String claimsbar(@ModelAttribute("qo")ClaimChartQueryObject qo, Model model){

        /*获取到查询出来的结果集*/
        List<Map<String, BigDecimal>> maps = chartService.queryByGroupType(qo);

        /*获取到qo的 分组字段 ,并且映射到 value ,共享给jsp , 用作副标题*/
        model.addAttribute("groupBy",ClaimChartQueryObject.groupBys.get(qo.getGroupType()));

       /* 横坐标:  遍历所有的结果集, 获取到其中的key为 groupBy 对应的value , 封装为list ,转换为json字符串,共享给表单
        纵坐标:  遍历所有的结果集, 获取到其中的key为 totalAmount 对应的value , 封装为list ,转换为json字符串,共享给表单 */
        List groupBys = new LinkedList();
        List totalAmounts = new LinkedList();

        for (Map<String, BigDecimal> map : maps) {
            groupBys.add(map.get("groupType"));
            totalAmounts.add(map.get("totalNumber"));
        }
        model.addAttribute("groupBys", JSON.toJSONString(groupBys));
        model.addAttribute("totalAmounts", JSON.toJSONString(totalAmounts));

        return "reportform/claimsbar";
    }

    @RequestMapping("claimspie")
    public String claimspie(@ModelAttribute("qo")ClaimChartQueryObject qo, Model model){
        /*获取到查询出来的结果集*/
        List<Map<String, BigDecimal>> maps = chartService.queryByGroupType(qo);

        /*获取到qo的 分组字段 ,并且映射到 value ,共享给jsp , 用作副标题*/
        model.addAttribute("groupBy",ClaimChartQueryObject.groupBys.get(qo.getGroupType()));

        /*循环遍历结果集, 查询出分组字段对应的值, 用作name , totalAmount对应的值, 用作value , 存储到新的map之中 ,
         所有的map存储到list之中 , 转换为jsonString ,共享给jsp*/
        List<Map<String,Object>> totalAmounts = new LinkedList();
        BigDecimal max = new BigDecimal(0);
        for (Map<String, BigDecimal> map : maps) {
            /*  {value:335, name:'直接访问'},*/
            if (max.compareTo((BigDecimal)(map.get("totalNumber")))==-1) {
                max = (BigDecimal)(map.get("totalNumber"));
            }
            LinkedHashMap<String, Object> value  = new LinkedHashMap<>();
            value.put("value",map.get("totalNumber"));
            value.put("name",map.get("groupType"));
            totalAmounts.add(value);
        }
        model.addAttribute("max",max);
        model.addAttribute("totalAmounts", JSON.toJSONString(totalAmounts));
        return "reportform/claimspie";
    }


}
